﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class admin_MyUserControls_TH_NguoiDungAdmin : System.Web.UI.UserControl
{
    private DataSet objDataSet;
    protected void Page_Load(object sender, EventArgs e)
    {
        // Load the grid only the first time the page is loaded
        if (!Page.IsPostBack)
        {
            // Load the categories grid
            BindGrid();
        }
    }

    // Populate the GridView with data
    private void BindGrid()
    {
        grid.DataSource = Membership.GetAllUsers();
        grid.DataBind();  
    }
    protected void grid_RowEditing(object sender, GridViewEditEventArgs e)
    {
        // Set the row for which to enable edit mode
        grid.EditIndex = e.NewEditIndex;
        // Set status message 
        // Reload the grid
        BindGrid();
    }
    protected void grid_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        // Cancel edit mode
        grid.EditIndex = -1;
        // Set status message
        // Reload the grid
        BindGrid();
    }
    protected void grid_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        // Retrieve updated data
        
    }
    protected void grid_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        // Get the ID of the record to be deleted
        string username = grid.DataKeys[e.RowIndex].Value.ToString();
        LoaiSanPhamBUS objLoaiSanPham = new LoaiSanPhamBUS(TGDDConfiguration.DbConnectionString);
        try
        {
            Membership.DeleteUser(username);
            BindGrid();
        }
        catch (System.Exception ex)
        {
            throw new Exception(ex.Message, ex.InnerException);
        }
    }
    protected void grid_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            LinkButton l = (LinkButton)e.Row.FindControl("lnkbtnDelete");
            l.Attributes.Add("onclick", "javascript:return " +
            "confirm('Bạn có chắc chắn muốn xóa không?' )");

            Label lblUsername = (Label)e.Row.FindControl("lblUsername");
            Label lblRole = (Label)e.Row.FindControl("lblNhom");
            string[] roles = Roles.GetRolesForUser(lblUsername.Text);
            lblRole.Text = roles[0];
        }
    }
}
